package com.platform.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.util.wechat.WechatRefundApiResult;
import com.platform.util.wechat.WechatUtil;
import com.platform.utils.Query;
import com.platform.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 *
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 * 描述: ApiIndexController <br>
 */
@Api(tags = "订单相关")
@RestController
@RequestMapping("/sys/transport")
public class ApiTransportController extends ApiBaseAction {
    @Autowired
    private ApiTransportService apiTransportService;
    @Autowired
    private ApiOrderService apiOrderService;
    @Autowired
    private ApiOrderGoodsService orderGoodsService;
    @Autowired
    private ApiKdniaoService apiKdniaoService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询配送信息列表
     * @param transportType
     * @param transportState
     * @return
     */
    @GetMapping("listByState")
    public Object listTransport(Integer transportType
            , Integer transportState) {
        List<Transport> list = apiTransportService.list(Wrappers.<Transport>lambdaQuery()
                .eq(!ObjectUtil.isEmpty(transportType), Transport::getTransportType, transportType)
                .eq(!ObjectUtil.isEmpty(transportState), Transport::getTransportState, transportState)
                .orderByDesc(Transport::getCreateTime)
        );
        List<OrderVo> objects = CollUtil.newArrayList();
        list.forEach(transport -> {
            OrderVo orderVo = apiOrderService.queryObject(transport.getOrderId());
            if (ObjectUtil.isEmpty(orderVo)) {
                return;
            }
            Map orderGoodsParam = new HashMap();
            orderGoodsParam.put("order_id", orderVo.getId());
            //添加所有商品
            List<OrderGoodsVo> goodsList = orderGoodsService.queryList(orderGoodsParam);
            orderVo.setGoodsVoList(goodsList);
            if (!CollUtil.isEmpty(goodsList)) {
                Integer goodsCount = 0;
                for (OrderGoodsVo orderGoodsEntity : goodsList) {
                    goodsCount += orderGoodsEntity.getNumber();
                    orderVo.setGoodsCount(goodsCount);
                }
            }
            if(!ObjectUtil.isEmpty(transport.getSysUserId())){
                //添加配送人信息
                SysUserEntity sysUserEntity = sysUserService.queryObject(new Long(transport.getSysUserId()));
                if (!ObjectUtil.isEmpty(sysUserEntity)) {
                    orderVo.setSysUserEntity(sysUserEntity);
                }
            }
            orderVo.setTransport(transport);
            objects.add(orderVo);
        });
        return toResponsSuccess(objects);
    }

    /**
     * 查询[配送信息
     * @param transportId
     * @return
     */
    @GetMapping("getById/{transportId}")
    public Object listTransport(@PathVariable  Integer transportId) {
        Transport transport = apiTransportService.getById(transportId);
        OrderVo orderVo = apiOrderService.queryObject(transport.getOrderId());
        if (ObjectUtil.isEmpty(orderVo)) {
            return toResponsFail("查询订单失败");
        }
        Map orderGoodsParam = new HashMap();
        orderGoodsParam.put("order_id", orderVo.getId());
        //添加所有商品
        List<OrderGoodsVo> goodsList = orderGoodsService.queryList(orderGoodsParam);
        orderVo.setGoodsVoList(goodsList);
        if (!CollUtil.isEmpty(goodsList)) {
            Integer goodsCount = 0;
            for (OrderGoodsVo orderGoodsEntity : goodsList) {
                goodsCount += orderGoodsEntity.getNumber();
                orderVo.setGoodsCount(goodsCount);
            }
        }
        //添加配送人信息
        if(!ObjectUtil.isEmpty(transport.getSysUserId())){
            //添加配送人信息
            SysUserEntity sysUserEntity = sysUserService.queryObject(new Long(transport.getSysUserId()));
            if (!ObjectUtil.isEmpty(sysUserEntity)) {
                orderVo.setSysUserEntity(sysUserEntity);
            }
        }
        orderVo.setTransport(transport);
        return toResponsSuccess(orderVo);
    }

    /**
     * 开始同城配送,修改配送状态
     * @param transportId
     * @return
     */
    @GetMapping("startTransport/{transportId}")
    public Object startTransport(@PathVariable  Integer transportId){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Transport byId = apiTransportService.getById(transportId);
        if(ObjectUtil.isEmpty(byId)){
            return toResponsFail("无该同城配送订单信息");
        }
        boolean update = apiTransportService.update(Wrappers.<Transport>lambdaUpdate()
                .set(Transport::getSysUserId,userEntity.getUserId())
                .set(Transport::getTransportState, 1)
                .set(Transport::getStartTime ,new Date() )
                .eq(Transport::getId, transportId)
                .eq(Transport::getTransportType, 0)
        );
        if(!update){
            return toResponsMsgSuccess("修改配送状态失败");
        }
        OrderVo orderVo = apiOrderService.queryObject(byId.getOrderId());
        orderVo.setOrder_status(300);
        orderVo.setShipping_status(1);
        apiOrderService.update(orderVo);
        return toResponsMsgSuccess("修改配送状态成功");
    }

    /**
     * 结束同城配送,修改配送状态
     * @param transportId
     * @return
     */
    @GetMapping("endTransport/{transportId}")
    public Object endTransport(@PathVariable  Integer transportId){
        Transport byId = apiTransportService.getById(transportId);
        if(ObjectUtil.isEmpty(byId)){
            return toResponsFail("无改同城配送订单信息");
        }
        boolean update = apiTransportService.update(Wrappers.<Transport>lambdaUpdate()
                .set(Transport::getTransportState, 2)
                .set(Transport::getEndTime ,new Date())
                .eq(Transport::getId, transportId)
                .eq(Transport::getTransportType, 0)
        );
        if(!update){
            return toResponsMsgSuccess("修改配送状态失败");
        }
        return toResponsMsgSuccess("修改配送状态成功");
    }

}
